package com.clkj.cldisabledmanclient.mvp.homerecovertrain;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.HomeTrainBean;
import com.clkj.cldisabledmanclient.bean.MyRecoverTrainBean;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.constant.TipStr;
import com.clkj.cldisabledmanclient.mvp.myrecovertrain.MyRecoverTrainContract;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HomeRecoverTrainActivity extends BaseActivity implements HomeRecoverTrainContract.View, PullToRefreshBase.OnRefreshListener2 {

    private PullToRefreshListView lv;
    private RelativeLayout rlEmpty;
    private TextView tvTipEmpty;
    private TextView tvStartTrain;
    private TextView tvEndTrain;

    private List<HomeTrainBean> homeTrainBeanList = new ArrayList<>();
    private JayLvBaseAdapter<HomeTrainBean> mAdapter;
    /**
     * 刷新标识
     */
    boolean isRefresh = false;
    /**
     * 是否可以加载更多标识
     */
    boolean canLoad = false;
    private Integer mPageIndex = 1;

    private HomeRecoverTrainContract.Presenter mPresenter;

    private String idCard;
    private ProgressDialog progressDialog;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_recover_train);
        initTitle(null, null, "居家康复训练", true, null);

        initView();
        initData();
        assignView();

        getHomeTrainListList();
    }

    @Override
    public void initData() {

        idCard = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        mPresenter = new HomeRecoverTrainPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

        lv.setOnRefreshListener(this);

        mAdapter = new JayLvBaseAdapter<HomeTrainBean>(this, homeTrainBeanList, R.layout.item_home_recover_train) {
            @Override
            public void convert(ViewHolder helper, int position, HomeTrainBean item) {

                helper.setText(R.id.tv_traintime, item.getDate() + "日训练记录");
                helper.setText(R.id.tv_srartTime, item.getStartTime());
                if (TextUtils.isEmpty(item.getEndTime()))
                    helper.setText(R.id.tv_endTime, "未结束");
                else
                    helper.setText(R.id.tv_endTime, item.getEndTime());

                if (TextUtils.isEmpty(item.getLongTime()))
                    helper.setText(R.id.tv_longtime, "未结束");
                else
                    helper.setText(R.id.tv_longtime, item.getLongTime());
            }
        };
        lv.setAdapter(mAdapter);

        //开始训练
        tvStartTrain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                type = "0";
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String today = dateFormat.format(date);
                mPresenter.judgeButton(idCard, "0", today);
                /*Intent toSubmit = new Intent(HomeRecoverTrainActivity.this, SubmitHomeTrainActivity.class);
                toSubmit.putExtra("type", "0");
                startActivity(toSubmit);*/
            }
        });

        //结束训练
        tvEndTrain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                type = "1";
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String today = dateFormat.format(date);
                mPresenter.judgeButton(idCard, "1", today);
              /*  Intent toSubmit = new Intent(HomeRecoverTrainActivity.this, SubmitHomeTrainActivity.class);
                toSubmit.putExtra("type", "1");
                startActivity(toSubmit);*/
            }
        });

        //点击进入详情
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent it = new Intent(HomeRecoverTrainActivity.this, HomeTrainDetailActivity.class);
                it.putExtra("trainDate", homeTrainBeanList.get(i - 1).getDate());
                startActivity(it);
            }
        });
    }

    @Override
    public void initView() {

        lv = (PullToRefreshListView) findViewById(R.id.lv);
        rlEmpty = (RelativeLayout) findViewById(R.id.rl_empty);
        tvTipEmpty = (TextView) findViewById(R.id.tv_tip_empty);
        tvStartTrain = (TextView) findViewById(R.id.tv_startTrain);
        tvEndTrain = (TextView) findViewById(R.id.tv_endTrain);
    }


    @Override
    public void getHomeTrainListList() {

        mPresenter.getHomeTrainList(idCard, mPageIndex.toString(), Config.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void getHomeTrainListListSuccess(List<HomeTrainBean> homeTrainBeans) {

        lv.onRefreshComplete();

        if (isRefresh) {
            homeTrainBeanList.clear();
            isRefresh = false;
        }

        if (homeTrainBeans.size() > 0) {

            if (homeTrainBeans.size() >= Config.DEFAULT_PAGE_SIZE) {
                lv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            homeTrainBeanList.addAll(homeTrainBeans);

        } else {
            canLoad = false;
            lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (homeTrainBeanList.size() == 0) {
                lv.setEmptyView(rlEmpty);
                tvTipEmpty.setText("未查询到您的居家康复数据");
            } else {
                ToastUtil.showShort(this, TipStr.TIP_NOT_MORE_DATAS);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getHomeTrainListListError(String tip) {

        lv.onRefreshComplete();
        if (isRefresh) {
            homeTrainBeanList.clear();
            isRefresh = false;
        }
        ToastUtil.showShort(this, tip);
        canLoad = false;
        lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        if (homeTrainBeanList.size() == 0) {
            lv.setEmptyView(rlEmpty);
        }
    }

    @Override
    public void judgeButtonSuccess(String result) {

        Intent toSubmit = new Intent(HomeRecoverTrainActivity.this, SubmitHomeTrainActivity.class);
        toSubmit.putExtra("type", type);
        startActivity(toSubmit);
    }

    @Override
    public void judgeButtonError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(HomeRecoverTrainContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        //设置点击进度对话框外的区域对话框不消失
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex = 1;
        isRefresh = true;
        getHomeTrainListList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex++;
        getHomeTrainListList();
    }
}
